/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/

#include "FTPushButton.h"

#define MIN_BUTTON_HEIGHT 48

#define FTB_DEFAULT_BACKGROUND QBrush( QColor( 75,75,75 ), Qt::Dense4Pattern)
#define FTB_DEFAULT_FOREGROUND QPen( QColor("black"), 2 )

#define FTB_DISABLED_BACKGROUND QBrush( QColor( 220,220,220 ), Qt::DiagCrossPattern)
#define FTB_DISABLED_FOREGROUND QPen( QColor("gray"), 1 )

#define FTB_PRESSED_BACKGROUND QBrush( "red", Qt::Dense4Pattern)

#define FTB_TEXT_PEN QPen( QColor("white"), 2)

#define FTB_CHECKED_BACKGROUND QBrush( QColor(200,200,200), Qt::Dense4Pattern)
#define FTB_CHECKED_FOREGROUND QPen( QColor("black", 4) );

FTPushButton::FTPushButton(const QIcon& i, const QSize& s, QWidget *parent) :
    QWidget(parent)
{
    initialize();
    setIcon( i, s );
}

FTPushButton::FTPushButton(const QString& s, QWidget *parent) :
    QWidget(parent)
{
    initialize();
    setText(s);
}

FTPushButton::FTPushButton(QWidget *parent) :
    QWidget(parent)
{
    initialize();
}

void FTPushButton::initialize()
{
    use_icon = use_text = false;
    setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
    state = not_pressed;
    checked = is_checkable = false;
    setAttribute( Qt::WA_NoMousePropagation, true);
    button_size = QSize(MIN_BUTTON_HEIGHT,MIN_BUTTON_HEIGHT);
    icon_size = QSize(0,0);
    background = FTB_DEFAULT_BACKGROUND;
    pressed_background = FTB_PRESSED_BACKGROUND;
    checked_background = FTB_CHECKED_BACKGROUND;
    text_pen = FTB_TEXT_PEN;
    border_pen = FTB_DEFAULT_FOREGROUND;
}

void FTPushButton::setIcon(const QIcon & i , const QSize & s)
{
    use_icon = true;
    icon = i;
    button_size = s;
    icon_size = s;
    if ( button_size.width() < icon_size.width() )
        button_size.setWidth( icon_size.width() );
    if ( button_size.height() < icon_size.height() )
        button_size.setHeight( icon_size.height() );
    resize( button_size );
}

void FTPushButton::setText(const QString & s )
{
    setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
    use_text = true;
    text = s;
    update();
 /*   QFontMetrics fm(font());
    button_size = fm.size( Qt::TextSingleLine, text )+QSize( 2,2 );

    if ( button_size.width() < 48 )
        button_size.setWidth( 48 );
    if ( button_size.height() < MIN_BUTTON_HEIGHT )
        button_size.setHeight( MIN_BUTTON_HEIGHT );
    resize( button_size );*/
}

QSize FTPushButton::sizeHint() const
{
    return button_size;
}

QSize FTPushButton::minimumSizeHint() const
{
    return sizeHint();
}

void FTPushButton::mousePressEvent ( QMouseEvent * event )
{
    setMouseTracking(true);
    state = pressed;
    update();
}

void FTPushButton::mouseReleaseEvent ( QMouseEvent * event )
{
    setMouseTracking(false);
    state = not_pressed;
    if ( is_checkable )
        checked = !checked;
    emit clicked( checked );
    update();
}

void FTPushButton::setBackground( const QBrush& def, const QBrush& pres, const QBrush& che )
{
    if ( def != QBrush() )
        background = def;
    if ( pres != QBrush() )
        pressed_background = pres;
    if ( che != QBrush() )
        checked_background = che;
    update();
}

void FTPushButton::setForeground( const QPen& p )
{
    text_pen = p;
    update();
}

void FTPushButton::paintEvent ( QPaintEvent * event )
{
    QPainter painter(this);

    // This is the border of the button
    QRect border = rect().adjusted(0,0,-1,-1);

    if ( isEnabled() )
    {
        // A pressed button gets the pressed background:
        if ( state == pressed )
            painter.fillRect( border, pressed_background );
        // A checked (if checkable) button requires the checked background...
        else if ( checked && is_checkable )
            painter.fillRect( border, checked_background );
        // all other buttons go with the standard background
        else
            painter.fillRect(border, background);
        // Set default pen for the border and draw the border
        painter.setPen( border_pen );
    }
    else
    {
        painter.fillRect( border, FTB_DISABLED_BACKGROUND );
        // Set default pen for the border and draw the border
        painter.setPen( FTB_DISABLED_FOREGROUND );
    }

    painter.drawRect( border );

    // If there is an icon...
    if ( use_icon )
        // Paint the icon!
        icon.paint( &painter, rect().adjusted( 3,3,-3,-3 ), (use_text ? Qt::AlignLeft : Qt::AlignCenter) | Qt::AlignVCenter);
    // If there is some text to print...
    if ( use_text && (text != "") )
    {
        // Set the proper pen
        painter.setPen( text_pen );
        // Move the text right if there is an icon
        border.setLeft( border.left() + icon_size.width() );
        // Draw the text
        painter.drawText( border,Qt::AlignCenter, text );
    }
}

bool FTPushButton::isCheckable() const
{
    return is_checkable;
}

void FTPushButton::setCheckable( bool c )
{
    is_checkable = c;
    update();
}

void FTPushButton::setChecked( bool c )
{
    checked = c;
    update();
}

bool FTPushButton::isChecked() const
{
    return checked;
}

void FTPushButton::click()
{
    checked = !checked;
    update();
    emit clicked( checked );
}
